package com.hexiang.utils;

import com.hexiang.response.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.async.WebAsyncTask;

import java.util.concurrent.Callable;

@Slf4j
public class WebAsyncTaskUtils {

    /**
     * 移除初始化
     *
     * @param callable
     * @return
     */
    public static WebAsyncTask instance(long timeout, Callable callable) {
        WebAsyncTask webAsyncTask = new WebAsyncTask<>(timeout, callable);

        webAsyncTask.onCompletion(() -> log.info("Web async task completed"));
        webAsyncTask.onError(() -> {
            log.error("Web async task error");
            return CommonResponse.builder().success(false).message("task error").build();
        });
        webAsyncTask.onTimeout(() -> {
            log.info("Web async task timed out");
            return CommonResponse.builder().success(false).message("task timed out").build();
        });
        return webAsyncTask;
    }
}